package com.test.multithread.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.atomic.AtomicReference;

//加法计数器
public class WtCyclicBarrierDemo {
    public static void main(String[] args) {
        AtomicReference<Integer> temp = new AtomicReference<>(0);
        /**
         * 集齐5名队员，开始游戏
         */
        // 开始战斗的线程
        CyclicBarrier cyclicBarrier = new CyclicBarrier(5,()->{
            System.out.println("欢迎来到王者荣耀，敌军还有五秒到达战场！全军出击！");
        });
        for (int i = 1; i <=5 ; i++) {
            // lambda能操作到 i 吗
            new Thread(()->{
                System.out.println(Thread.currentThread().getName()+"第"+temp.getAndSet(temp.get() + 1)+"个进入游戏！");
                try {
                    cyclicBarrier.await(); // 等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }
}
